use crate::database::Database;
use uuid::Uuid;

pub struct SubmissionHandler {
    db: Database
}

impl SubmissionHandler {
    pub fn new(db: Database) -> Self {
        Self { db }
    }

    pub async fn create_submission(
        &self,
        user_id: &str,
        package_data: &[u8]
    ) -> Result<String, String> {
        let package_hash = format!("{:x}", md5::compute(package_data));
        let submission_id = Uuid::new_v4().to_string();

        sqlx::query!(
            r#"
            INSERT INTO submissions 
            (id, user_id, package_hash, status)
            VALUES (?, ?, ?, 'pending')
            "#,
            submission_id,
            user_id,
            package_hash
        )
        .execute(self.db.get_pool())
        .await
        .map_err(|e| format!("提交失败: {}", e))?;

        Ok(submission_id)
    }
}